﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExtBusiness.CommonBusiness;
using ExtBusiness.Enum;

namespace ExtBusiness.WepPost
{
    public static class HttpHelper
    {
        public static readonly String GZipAcceptEncoding = "gzip";

        public static byte[] UnZip(byte[] GZipBytes, String ContentEncoding)
        {
            if (String.IsNullOrWhiteSpace(ContentEncoding) || ContentEncoding.IndexOf(GZipAcceptEncoding) < 0)
            {
                return GZipBytes;
            }

            return GZIP.UnGZIP(GZipBytes);
        }

        /// <summary>
        /// Win下定义的编码方式代码转换为HTTP的代码
        /// </summary>
        /// <param name="Charset"></param>
        /// <returns></returns>
        public static String WinConvertToHttpCharset(EnumCharsetType Charset)
        {
            switch (Charset)
            {
                case EnumCharsetType.UTF8:
                    return "utf-8";
                case EnumCharsetType.GBK:
                    return "gbk";
                case EnumCharsetType.GB2312:
                    return "gb2312";
                default:
                    return "gb2312";
            }
        }

        /// <summary>
        /// HTTP的编码方式代码转换为WIN的代码
        /// </summary>
        /// <param name="Charset"></param>
        /// <returns></returns>
        public static EnumCharsetType HttpConvertToWinCharset(String Charset)
        {
            var charset = Charset == null ? string.Empty : Charset.ToLower().Trim();
            switch (charset)
            {
                case "utf-8":
                    return EnumCharsetType.UTF8;
                case "gbk":
                    return EnumCharsetType.GBK;
                case "gb2312":
                    return EnumCharsetType.GB2312;
                default:
                    return EnumCharsetType.GB2312;
            }
        }
    }
}
